<template>
  <div class="register-container">
    <div class="register-left">
      <div class="brand-section">
        <div class="logo-container">
          <div class="logo-icon">⭐</div>
          <h1 class="brand-title">Sparkie<br>Interview</h1>
        </div>
        <p class="brand-subtitle">AI 驱动的智能面试平台</p>
        <div class="feature-list">
          <div class="feature-item">
            <span class="feature-icon">🎯</span>
            <span class="feature-text">智能面试评估</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">📊</span>
            <span class="feature-text">实时数据分析</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">🔒</span>
            <span class="feature-text">安全可靠的平台</span>
          </div>
        </div>
      </div>
      <div class="decoration">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
      </div>
    </div>
    
    <div class="register-right">
      <div class="register-form-container">
        <div class="form-header">
          <h2 class="form-title">创建账号</h2>
          <p class="form-subtitle">加入Sparkie Interview，开启智能面试体验</p>
        </div>
        
        <el-form
          ref="registerFormRef"
          :model="registerForm"
          :rules="registerRules"
          class="register-form"
          @submit.prevent="handleRegister"
        >
          <!-- 基本信息 -->
          <div class="form-section">
            <h4 class="section-title">基本信息</h4>
            
            <el-form-item prop="name">
              <el-input
                v-model="registerForm.name"
                placeholder="真实姓名"
                prefix-icon="User"
                size="large"
                class="form-input"
              />
            </el-form-item>
            
            <el-form-item prop="email">
              <el-input
                v-model="registerForm.email"
                placeholder="邮箱地址"
                prefix-icon="Message"
                size="large"
                class="form-input"
              />
            </el-form-item>
            
            <el-form-item prop="phone">
              <el-input
                v-model="registerForm.phone"
                placeholder="手机号码（可选）"
                prefix-icon="Phone"
                size="large"
                class="form-input"
              />
            </el-form-item>
            
            <el-form-item prop="gender">
              <el-select
                v-model="registerForm.gender"
                placeholder="请选择性别"
                size="large"
                class="form-input"
              >
                <el-option label="男" value="male" />
                <el-option label="女" value="female" />
              </el-select>
            </el-form-item>
          </div>
          
          <!-- 密码设置 -->
          <div class="form-section">
            <h4 class="section-title">密码设置</h4>
            
            <el-form-item prop="password">
              <el-input
                v-model="registerForm.password"
                type="password"
                placeholder="设置密码"
                prefix-icon="Lock"
                size="large"
                show-password
                class="form-input"
              />
              <div class="password-strength">
                <div class="strength-bar">
                  <div 
                    class="strength-fill" 
                    :class="passwordStrength.level"
                    :style="{ width: passwordStrength.percentage + '%' }"
                  ></div>
                </div>
                <span class="strength-text">{{ passwordStrength.text }}</span>
              </div>
            </el-form-item>
            
            <el-form-item prop="confirmPassword">
              <el-input
                v-model="registerForm.confirmPassword"
                type="password"
                placeholder="确认密码"
                prefix-icon="Lock"
                size="large"
                show-password
                class="form-input"
              />
            </el-form-item>
          </div>
          
          <!-- 角色选择 -->
          <div class="form-section">
            <h4 class="section-title">选择身份</h4>
            
            <el-form-item prop="role">
              <div class="role-selection">
                <div 
                  class="role-card"
                  :class="{ active: registerForm.role === 'hr' }"
                  @click="registerForm.role = 'hr'"
                >
                  <el-radio v-model="registerForm.role" value="hr" class="role-radio">
                    <div class="role-content">
                      <el-icon class="role-icon"><UserFilled /></el-icon>
                      <div class="role-info">
                        <span class="role-text">HR 招聘官</span>
                        <span class="role-desc">创建面试、管理候选人、查看报告</span>
                      </div>
                    </div>
                  </el-radio>
                </div>
                <div 
                  class="role-card"
                  :class="{ active: registerForm.role === 'candidate' }"
                  @click="registerForm.role = 'candidate'"
                >
                  <el-radio v-model="registerForm.role" value="candidate" class="role-radio">
                    <div class="role-content">
                      <el-icon class="role-icon"><User /></el-icon>
                      <div class="role-info">
                        <span class="role-text">应聘者/候选人</span>
                        <span class="role-desc">参加面试、练习技能、查看反馈</span>
                      </div>
                    </div>
                  </el-radio>
                </div>
              </div>
            </el-form-item>
          </div>
          
          <!-- 协议同意 -->
          <el-form-item prop="agreements">
            <el-checkbox-group v-model="registerForm.agreements" class="agreements-group">
              <el-checkbox value="terms" class="agreement-checkbox">
                我已阅读并同意 <el-link type="primary" @click="showTerms">《用户协议》</el-link>
              </el-checkbox>
              <el-checkbox value="privacy" class="agreement-checkbox">
                我已阅读并同意 <el-link type="primary" @click="showPrivacy">《隐私政策》</el-link>
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          
          <el-form-item>
            <el-button
              type="primary"
              size="large"
              class="register-button"
              :loading="isLoading"
              @click="handleRegister"
            >
              立即注册
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="form-footer">
          <span class="footer-text">已有账号？</span>
          <el-link type="primary" class="footer-link" @click="goToLogin">立即登录</el-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { authService } from '@/services/auth'
import { ElMessage } from 'element-plus'
import { UserFilled, User } from '@element-plus/icons-vue'

export default {
  name: 'RegisterView',
  components: {
    UserFilled,
    User
  },
  setup() {
    const router = useRouter()
    const authStore = useAuthStore()
    const registerFormRef = ref()
    const isLoading = ref(false)

    const registerForm = reactive({
      name: '',
      email: '',
      phone: '',
      gender: 'female', // 默认选择女性
      password: '',
      confirmPassword: '',
      role: 'candidate',
      agreements: []
    })

    // 密码强度计算
    const passwordStrength = computed(() => {
      const password = registerForm.password
      if (!password) {
        return { level: '', percentage: 0, text: '' }
      }

      let score = 0
      let checks = 0

      // 长度检查
      if (password.length >= 8) {
        score += 25
        checks++
      }
      
      // 包含数字
      if (/\d/.test(password)) {
        score += 25
        checks++
      }
      
      // 包含小写字母
      if (/[a-z]/.test(password)) {
        score += 25
        checks++
      }
      
      // 包含大写字母或特殊字符
      if (/[A-Z]/.test(password) || /[^A-Za-z0-9]/.test(password)) {
        score += 25
        checks++
      }

      let level = ''
      let text = ''
      
      if (score < 50) {
        level = 'weak'
        text = '弱'
      } else if (score < 75) {
        level = 'medium'
        text = '中等'
      } else {
        level = 'strong'
        text = '强'
      }

      return { level, percentage: score, text }
    })

    const registerRules = {
      name: [
        { required: true, message: '请输入真实姓名', trigger: 'blur' },
        { min: 2, max: 50, message: '姓名长度在2-50个字符', trigger: 'blur' }
      ],
      email: [
        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
      ],
      phone: [
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
      ],
      gender: [
        { required: true, message: '请选择性别', trigger: 'change' }
      ],
      password: [
        { required: true, message: '请设置密码', trigger: 'blur' },
        { min: 8, message: '密码长度不能少于8位', trigger: 'blur' },
        {
          validator: (rule, value, callback) => {
            if (value && !/(?=.*[a-zA-Z])(?=.*\d)/.test(value)) {
              callback(new Error('密码必须包含字母和数字'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }
      ],
      confirmPassword: [
        { required: true, message: '请确认密码', trigger: 'blur' },
        {
          validator: (rule, value, callback) => {
            if (value !== registerForm.password) {
              callback(new Error('两次输入的密码不一致'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }
      ],
      role: [
        { required: true, message: '请选择身份角色', trigger: 'change' }
      ],
      agreements: [
        {
          validator: (rule, value, callback) => {
            if (!value.includes('terms') || !value.includes('privacy')) {
              callback(new Error('请阅读并同意相关协议'))
            } else {
              callback()
            }
          },
          trigger: 'change'
        }
      ]
    }

    const handleRegister = async () => {
      try {
        const valid = await registerFormRef.value.validate()
        if (!valid) return

        isLoading.value = true
        
        // 调用注册API
        const registerData = {
          name: registerForm.name,
          email: registerForm.email,
          phone: registerForm.phone || '',
          gender: registerForm.gender,
          password: registerForm.password,
          password_confirm: registerForm.confirmPassword,
          role: registerForm.role
        }

        const response = await authStore.register(registerData)
        
        ElMessage.success('注册成功！欢迎使用Sparkie Interview')
        
        // 根据角色跳转到相应页面
        const redirectPath = registerForm.role === 'hr' ? '/hr/dashboard' : '/candidate/dashboard'
        router.push(redirectPath)
        
      } catch (error) {
        console.error('注册失败:', error)
        ElMessage.error(error.response?.data?.message || '注册失败，请重试')
      } finally {
        isLoading.value = false
      }
    }

    const goToLogin = () => {
      router.push('/login')
    }

    const showTerms = () => {
      ElMessage.info('用户协议功能开发中...')
    }

    const showPrivacy = () => {
      ElMessage.info('隐私政策功能开发中...')
    }

    return {
      registerFormRef,
      registerForm,
      registerRules,
      passwordStrength,
      isLoading,
      handleRegister,
      goToLogin,
      showTerms,
      showPrivacy
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  min-height: 100vh;
}

.register-left {
  flex: 2;
  background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.brand-section {
  text-align: center;
  color: white;
  z-index: 2;
}

.logo-container {
  margin-bottom: 24px;
}

.logo-icon {
  font-size: 4rem;
  margin-bottom: 16px;
}

.brand-title {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 16px;
  line-height: 1.2;
}

.brand-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  font-weight: 500;
  margin-bottom: 32px;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
  align-items: flex-start;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 12px;
}

.feature-icon {
  font-size: 1.5rem;
}

.feature-text {
  font-size: 1rem;
  opacity: 0.9;
}

.decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.shape {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
}

.shape-1 {
  width: 300px;
  height: 300px;
  top: -150px;
  left: -150px;
}

.shape-2 {
  width: 200px;
  height: 200px;
  bottom: -100px;
  right: -100px;
}

.shape-3 {
  width: 150px;
  height: 150px;
  top: 50%;
  right: -75px;
  transform: translateY(-50%);
}

.register-right {
  flex: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  background-color: var(--bg-color);
  overflow-y: auto;
}

.register-form-container {
  width: 100%;
  max-width: 500px;
}

.form-header {
  text-align: center;
  margin-bottom: 32px;
}

.form-title {
  font-size: 2rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.form-subtitle {
  color: var(--text-secondary);
  font-size: 1rem;
}

.register-form {
  margin-bottom: 24px;
}

.form-section {
  margin-bottom: 32px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--border-color);
}

.form-section:last-of-type {
  border-bottom: none;
  margin-bottom: 24px;
}

.section-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 20px;
}

.form-input {
  margin-bottom: 16px;
}

.password-strength {
  margin-top: 8px;
}

.strength-bar {
  height: 4px;
  background-color: var(--border-color);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 4px;
}

.strength-fill {
  height: 100%;
  transition: all 0.3s ease;
}

.strength-fill.weak {
  background-color: var(--error-color);
}

.strength-fill.medium {
  background-color: var(--warning-color);
}

.strength-fill.strong {
  background-color: var(--success-color);
}

.strength-text {
  font-size: 12px;
  color: var(--text-secondary);
}

.role-selection {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
}

.role-card {
  border: 2px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.role-card:hover {
  border-color: var(--primary-color);
  background-color: rgba(103, 83, 192, 0.05);
}

.role-card.active {
  border-color: var(--primary-color);
  background-color: rgba(103, 83, 192, 0.1);
}

.role-radio {
  width: 100%;
  margin: 0;
  padding: 20px;
}

.role-content {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
}

.role-icon {
  font-size: 32px;
  color: var(--primary-color);
}

.role-info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
}

.role-text {
  font-size: 16px;
  color: var(--text-primary);
  font-weight: 600;
}

.role-desc {
  font-size: 14px;
  color: var(--text-secondary);
}

.agreements-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.agreement-checkbox {
  margin: 0;
  font-size: 14px;
  color: var(--text-secondary);
}

.register-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
  border: none;
  border-radius: 8px;
}

.register-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(103, 83, 192, 0.3);
}

.form-footer {
  text-align: center;
}

.footer-text {
  color: var(--text-secondary);
  font-size: 14px;
  margin-right: 8px;
}

.footer-link {
  font-size: 14px;
  color: var(--primary-color);
  text-decoration: none;
}

.footer-link:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
  }
  
  .register-left {
    flex: none;
    height: 300px;
  }
  
  .register-right {
    flex: none;
    padding: 24px;
  }
  
  .role-selection {
    gap: 8px;
  }
  
  .role-content {
    flex-direction: column;
    text-align: center;
    gap: 8px;
  }
  
  .role-info {
    align-items: center;
  }
  
  .brand-title {
    font-size: 2rem;
  }
  
  .brand-subtitle {
    font-size: 1rem;
  }
  
  .feature-list {
    align-items: center;
  }
}
</style> 